package com.taysear.parcel.fieldfactory;

import com.taysear.parcel.ParcelApplication;
import com.taysear.parcel.domain.EmployeeStatus;
import com.taysear.parcel.domain.EmployeeType;
import com.taysear.parcel.domain.Gender;
import com.taysear.parcel.domain.Route;
import com.taysear.parcel.domain.RouteStatus;
import com.taysear.parcel.domain.Station;
import com.taysear.parcel.domain.StationStatus;
import com.taysear.parcel.domain.Substation;
import com.taysear.parcel.domain.Time;
import com.taysear.parcel.domain.helpers.Temp;
import com.taysear.parcel.util.Constants;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.Field;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;

@SuppressWarnings("serial")
public class RouteTimeFieldFactory extends FieldFactory {
	
	ParcelApplication app;
	public final TextField routeTimeId = new TextField("Route time id");
	NativeSelect route = new NativeSelect("Route");
	NativeSelect time = new NativeSelect("Time");
	NativeSelect isActive = new NativeSelect("Status");

	public RouteTimeFieldFactory(ParcelApplication app) {
		this.app = app;
	}

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f = null;
		if ("routeTimeId".equals(propertyId)) {
			routeTimeId.setRequired(true);
			routeTimeId.setWidth(Constants.COMMON_FIELD_WIDTH);
			return routeTimeId;
		} if ("isActive".equals(propertyId)) {
			BeanItemContainer<Temp> container = new BeanItemContainer<Temp>(Temp.class);
			container.addBean(new Temp(1, "Active"));
			container.addBean(new Temp(0, "InActive"));
			isActive.setContainerDataSource(container);
			isActive.setImmediate(true);
			isActive.setNullSelectionAllowed(false);
			isActive.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			isActive.setRequired(true);
			isActive.setWidth(Constants.COMMON_FIELD_WIDTH);
			isActive.setRequiredError(isActive.getCaption()+" is required");
			return isActive;
		} if ("route".equals(propertyId)) {
			try{
				route = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Route> container = JPAContainerFactory.make(Route.class, app.PERSISTENCE_UNIT);
				route.setContainerDataSource(container);
			}
			route.setImmediate(true);
			route.setWidth(Constants.COMMON_FIELD_WIDTH);
			route.setNullSelectionAllowed(false);
			route.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			route.setRequired(true);
			route.setRequiredError(route.getCaption()+" is required");
			return route;
		} if ("time".equals(propertyId)) {
			try{
				time = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Time> container = JPAContainerFactory.make(Time.class, app.PERSISTENCE_UNIT);
				time.setContainerDataSource(container);
			}
			time.setImmediate(true);
			time.setWidth(Constants.COMMON_FIELD_WIDTH);
			time.setNullSelectionAllowed(false);
			time.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			time.setRequired(true);
			time.setRequiredError(time.getCaption()+" is required");
			return time;
		} else {
			// Use the super class to create a suitable field base on the
			// property type.
			f = super.createField(item, propertyId, uiContext);
		}
		return f;
	}
}
